<template>
    <div class="login">
        <el-form ref="loginForm" class="login-form">
            <h3 class="title">大事件后台管理系统</h3>
            <!-- 登录模块 -->
            <div v-show="status == 'login'">
                <el-form-item prop="user_name">
                    <el-input v-model="form.user_name" type="text" placeholder="账号">
                        <i slot="prefix" class="el-icon-user" />
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="form.password" type="password" placeholder="密码" @keyup.enter.native="loginHandler">
                        <i slot="prefix" class="el-icon-lock" />
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <div class="registerBtn" @click="status = 'register'">去注册</div>
                </el-form-item>
                <el-form-item style="width: 100%">
                    <el-button size="medium" type="primary" style="width: 100%" @click.native.prevent="loginHandler">登录</el-button>
                </el-form-item>
            </div>
            <!-- 注册模块 -->
            <div v-show="status == 'register'">
                <el-form-item prop="username">
                    <el-input v-model="form.user_name" type="text" placeholder="姓名" />
                </el-form-item>
                <el-form-item prop="nick_name">
                    <el-input v-model="form.nick_name" type="text" placeholder="昵称" />
                </el-form-item>
                <el-form-item prop="email">
                    <el-input v-model="form.email" type="text" placeholder="邮箱" />
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="form.password" type="password" placeholder="密码" />
                </el-form-item>
                <el-form-item>
                    <div class="registerBtn" @click="status = 'login'">返回登录</div>
                </el-form-item>
                <el-form-item style="width: 100%">
                    <el-button @click="registerHandler" size="medium" type="primary" style="width: 100%">注册</el-button>
                </el-form-item>
            </div>
        </el-form>
        <!--  底部  -->
        <div class="el-login-footer">
            <span>Copyright © 2018-2022 dsz All Rights Reserved.</span>
        </div>
    </div>
</template>

<script>
import { userRegister, userLogin } from '@/api'
export default {
    name: "Login",
    data() {
        return {
            status: 'login',
            form: {}
        }
    },
    created() {

    },
    methods: {
        // 注册
        async registerHandler() {
            let res = await userRegister(this.form)
            this.$message.success(res.message)
        },
        // 登录
        async loginHandler() {
            let res = await userLogin(this.form)
            this.$message.success(res.message)
            localStorage.setItem('token', res.data)
            this.$router.push('/')
        }
    }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    background: #666;
    .registerBtn {
        text-align: right;
        color: #409eff;
        cursor: pointer;
    }
}

.title {
    margin: 0px auto 30px auto;
    text-align: center;
    color: #707070;
}

.login-form {
    border-radius: 6px;
    background: #ffffff;
    width: 400px;
    padding: 25px 25px 5px 25px;

    .el-input__prefix {
        display: flex;
        flex-direction: column;
        justify-content: center;
    }

    .el-input {
        height: 38px;

        input {
            height: 38px;
        }
    }

    .input-icon {
        height: 39px;
        width: 14px;
        margin-left: 2px;
    }
}

.login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
}

.login-code {
    width: 33%;
    height: 38px;
    float: right;
    text-align: right;

    img {
        cursor: pointer;
        vertical-align: middle;
    }
}

.el-login-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
}

.login-code-img {
    height: 38px;
}
</style>